# coding=utf8
# 更新sql表字段
from app import create_app
from app.extensions import db,log
from app.modules.common.utils import DbCommit
from app.modules.project.models import ProjectSql
from app.modules.integration.resources import getIntegrationProjectInfoList

application = create_app(flask_config = 'local')


def getProjectName(integration_id, trunk_name):
    for pro in getIntegrationProjectInfoList(integration_id, None, True):
        if pro.trunk_name == trunk_name:
            return pro.project_name


def save():
    with application.app_context():
        for row in db.session.execute("select * from project_sql_bak"):
            sql = ProjectSql(project_name = getProjectName(dict(row)['integration_id'], dict(row)['trunk_name']),
                             integration_id = dict(row)['integration_id'], trunk_name = dict(row)['trunk_name'],
                             file_name = dict(row)['file_name'], file_path = dict(row)['file_path'],
                             is_dba_check = dict(row)['is_dba_check'], is_upload = dict(row)['is_upload'])
            log.info(sql.to_dict())
            db.session.add(sql)
            DbCommit()


if __name__ == '__main__':
    save()
    pass
